<?
/*
**2015-07-03 鼓浪屿轮渡
*/

include('/var/www/html/new/d/module/api/glyld/glyld.class.php');
include('/var/www/html/new/d/module/api/function.class.php');
$txt=json_encode($_REQUEST);
logfile(date('Y-m-d H:i:s').'---'.$txt);

$txt = '{"VenueID":"9F3A7072E74142D7A41D999318818922","RoundID":"9652EE647B814F9A8F79B4061E1FDB2B","AreaID":"D947FBA1F8F94D8FAF40D95007C0ACFA","SeatID":"2015-07-14","Tnum":"1","Fid":"27491","Ordern":"1484122","Action":"Relation"}';
$arr = json_decode($txt,true);

if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}
$gly = new Ferry_gly();
$PFT_infunction = new AllFunction();

if(!isset($arr['Action'])){
	$flightLineid = $_REQUEST['VenueID'];  //航线ID
	$ticketid = $_REQUEST['RoundID'];  //票类ID
	$dailyFlightGid = $_REQUEST['AreaID'];  //航班ID 
	$selectdate = str_replace('-','',$_REQUEST['SeatID']); //查询日期
	$select_count = $gly->getDailyFlight($flightLineid,$selectdate);
	$select_count_xml = simplexml_load_string($select_count);
	if((int)$select_count_xml->code == 0){
		foreach($select_count_xml->content->list->DailyFlight as $list){
			if((string)$list->dailyFlightGid == $dailyFlightGid){
				if((int)$list->offLineFreeCount > 0){
					logfile("200|0|0|0");
					exit("200|0|0|0");
				}
				else {
					logfile("1062|剩余座位不足");
					exit("1062|剩余座位不足");
				}
			}
		}
	}else {
		logfile("105|数据为空");
		exit("105|数据为空");
	}
}
elseif($arr['Action']=='Relation'){ //下单成功
	$submit_array = array(
		'flightLineid'=>$arr['VenueID'],			//航线ID
		'ticketid'=>$arr['RoundID'],				//票类ID
		'dailyFlightGid'=>$arr['AreaID'],			//航班ID
		'date'=>$arr['SeatID'],					//日期
		'Ordern'=>$arr['Ordern'], 					//订单号
		'Tnum'=>$arr['Tnum'],						//数量
	);
	$pft_order_xml = $PFT_infunction->Select_PFTorder($arr['Ordern']);
	$submit_array['ordername']=(string)$pft_order_xml->Rec->UUordername;
	$submit_array['IDcar'] =(string)$pft_order_xml->Rec->UUpersonid;
	$submit_array['tel'] = (string)$pft_order_xml->Rec->UUordertel;
	$UUpid=(int)$pft_order_xml->Rec->UUpid;	
	$UUgprice = $PFT_infunction->Get_price($UUpid,$arr['SeatID'],0); //供货价
	$submit_array['gprice']= $UUgprice/100;	
	$submit_order = $gly->saveSaleOrder($submit_array);
	$submit_order_xml = simplexml_load_string($submit_order);
	logfile($_REQUEST['Ordern']."|".$submit_order_xml);
	if((int)$submit_order_xml->code == 0){
		$updateRemote = $PFT_infunction->Remote_Ordernum_Update($_REQUEST['Ordern'],'glyld_'.(string)$submit_order_xml->content->saleOrder->sno);			
		//支付订单
		$update_order = $gly->payForSaleOrder((string)$submit_order_xml->content->saleOrder->sno);
		$update_order_xml = simplexml_load_string($update_order);
		logfile($_REQUEST['Ordern']."->".$update_order_xml);
		if((int)$update_order_xml->code == 0){
			$str = "update uu_order_fx_details set memo='glyld_pay_success' where orderid=".$_REQUEST['Ordern']." limit 1";
			$GLOBALS['le']->query($str);
		}
		exit("200|0|0|0");
	}else{
		logfile((string)$submit_order_xml->content);
		exit("124|".(string)$submit_order_xml->content);
	}
}
elseif($_REQUEST['Action'] == 'MOD'){//取消订单
	$pft_order_xml = $PFT_infunction->Select_PFTorder($_REQUEST['Ordern']);
	$UUremotenums = explode("_",(string)$pft_order_xml->Rec->UUremotenum);		
	$UUremotenum = $UUremotenums[1];//远端订单号
	$saveReturn = $gly->saveReturn($UUremotenum);
	$saveReturn_xml = simplexml_load_string($saveReturn);
	if((int)$saveReturn_xml->code == 0){			
		$str = "update uu_order_fx_details set memo='glyld_cancel_success' where orderid=".$_REQUEST['Ordern']." limit 1";
		$GLOBALS['le']->query($str);
		$poundageDescribe = $saveReturn_xml->content->saleOrder->poundageDescribe;//退款百分比
		$UUmid = (int)$pft_order_xml->Rec->UUmid; //分销商id
		$UUaid = (int)$pft_order_xml->Rec->UUaid; //供应商id
		$UUtprice = (float)$pft_order_xml->Rec->UUtprice; //结算价
		$UUtotalmoney = (float)$pft_order_xml->Rec->UUtotalmoney; 
		$Debit = ceil(($UUtprice * $poundageDescribe)/100);//扣款金额
		$modify_money = $PFT_infunction->PFT_Modify_Money($UUmid,$Debit,$action=0,$dmode=0,$dtype=0,$ptype=0,$ordern=$_REQUEST['Ordern']);
		logfile($_REQUEST['Ordern']."|".$Debit);
		exit("200|$poundageDescribe|0|0");
	}
	else{
		logfile((string)$saveReturn_xml->code."|".(string)$saveReturn_xml->content);
		exit((string)$saveReturn_xml->code."|".(string)$saveReturn_xml->content);
	}
}
function logfile($txt){
	$fp = fopen('/var/www/html/new/d/module/api/glyld/gly_get_data.txt',"a");
	flock($fp, LOCK_EX) ;
	fwrite($fp,$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp); 
}
?>